import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/const/mahjong_system_const.dart';
import 'package:freedom/entity/card/mahjong_card_entity.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';

class CardWidget {
  // 构建当前用户的手牌
  static Widget buildBottomCard(int? cardId, double cardHeight,
      {isClick = true, EdgeInsetsGeometry? margin}) {
    var backGroudWidth = cardHeight / MahjongContext().btCardWHRatio;
    return Container(
      height: cardHeight,
      width: backGroudWidth,
      margin: margin,
      child: Stack(
        children: [
          Container(
              width: double.infinity,
              height: double.infinity,
              child: Image.asset(
                "assets/images/mahjong/user/bottom/usercard_background.png",
                fit: BoxFit.cover,
              )),
          Align(
              alignment: const Alignment(0, 0.5),
              child: Container(
                  width: backGroudWidth * MahjongContext().cardImageWHRatio,
                  height: backGroudWidth,
                  child: Image.asset(
                    'assets/images/mahjong/card/south_hand_tile_' +
                        cardId.toString() +
                        '.png',
                    //width: entity.cardWidth,
                    fit: BoxFit.cover,
                  ))),
          isClick
              ? Container()
              : ClipRRect(
                  borderRadius: BorderRadius.circular(5.5),
                  child: Container(
                    width: double.infinity,
                    height: double.infinity,
                    color: Colors.black.withOpacity(0.5),
                  )),
        ],
      ),
    );
  }
  /**
   * 构建暗杠的样式
   */

  static Widget buildSuperpositionCard(int? cardInfo, double cardHeight,
      {bool dark = false}) {
    var cardChinHeight =
        cardHeight * (1 - MahjongContext().exportDiagonalRatio);
    var cardWidth = cardHeight / MahjongContext().btCardWHRatio;
    return Column(
      children: [
        Container(
          height: cardHeight + cardChinHeight,
          width: cardWidth,
          child: Stack(
            children: [
              Container(
                  width: cardWidth,
                  height: cardHeight,
                  margin: EdgeInsets.only(top: cardChinHeight),
                  child: Image.asset(
                    dark
                        ? "assets/images/mahjong/user/bottom/export_up.png"
                        : "assets/images/mahjong/user/bottom/export_background.png",
                    fit: BoxFit.cover,
                  )),
              Container(
                  width: cardWidth,
                  height: cardHeight,
                  child: Image.asset(
                    "assets/images/mahjong/user/bottom/export_background.png",
                    fit: BoxFit.cover,
                  )),
              cardInfo == null
                  ? Container()
                  : Align(
                      alignment: const Alignment(0, -0.5),
                      child: Container(
                          width:
                              cardWidth * MahjongContext().exportDiagonalRatio,
                          height: cardWidth *
                              MahjongContext().exportDiagonalRatio /
                              MahjongContext().cardImageWHRatio,
                          child: Image.asset(
                            'assets/images/mahjong/card/south_hand_tile_' +
                                cardInfo.toString() +
                                '.png',
                            fit: BoxFit.cover,
                          ))),
            ],
          ),
        ),
      ],
    );
  }

  /// 构建卡牌的widget   出牌的样式

  static Widget buildExportCardInfo(int? cardInfo, double cardHeight,
      {marginTop = true}) {
    var cardChinHeight =
        cardHeight * (1 - MahjongContext().exportDiagonalRatio);
    var cardWidth = cardHeight / MahjongContext().btCardWHRatio;
    return Column(
      children: [
        SizedBox(
          height: cardHeight + cardChinHeight,
          width: cardWidth,
          child: Stack(
            children: [
              Container(
                  width: cardWidth,
                  height: cardHeight,
                  margin: marginTop
                      ? EdgeInsets.only(top: cardChinHeight)
                      : const EdgeInsets.all(0),
                  child: Image.asset(
                    cardInfo == null
                        ? "assets/images/mahjong/user/bottom/export_up.png"
                        : "assets/images/mahjong/user/bottom/export_background.png",
                    fit: BoxFit.cover,
                  )),
              cardInfo == null
                  ? Container()
                  : Align(
                      alignment: const Alignment(0, -0.5),
                      child: Container(
                          margin: marginTop
                              ? EdgeInsets.only(top: cardChinHeight)
                              : const EdgeInsets.all(0),
                          width:
                              cardWidth * MahjongContext().exportDiagonalRatio,
                          height: cardWidth *
                              MahjongContext().exportDiagonalRatio /
                              MahjongContext().cardImageWHRatio,
                          child: Image.asset(
                            'assets/images/mahjong/card/south_hand_tile_$cardInfo.png',
                            fit: BoxFit.cover,
                          ))),
            ],
          ),
        ),
      ],
    );
  }
  // 构建当前用户出的手牌

  static Widget buildExportCard(MahjongCardEntity cardInfo, double cardWidth) {
    var backGroudHeight = cardWidth * MahjongSystemConst.cardWHScale;
    return Column(
      children: [
        Container(
          height: backGroudHeight,
          width: cardWidth,
          child: Stack(
            children: [
              Container(
                  width: double.infinity,
                  height: double.infinity,
                  child: Image.asset(
                    cardInfo.cardId == null
                        ? "assets/images/mahjong/user/bottom/export_up.png"
                        : cardInfo.resv
                            ? "assets/images/mahjong/user/bottom/export_re.png"
                            : "assets/images/mahjong/user/bottom/export_background.png",
                    fit: BoxFit.cover,
                  )),
              cardInfo.highlight
                  ? Container(
                      width: double.infinity,
                      height: double.infinity,
                      color: Colors.blue.withOpacity(0.3),
                    )
                  : SizedBox(),
              cardInfo.cardId == null
                  ? Container()
                  : Align(
                      alignment: cardInfo.resv
                          ? const Alignment(0, 0.5)
                          : const Alignment(0, -0.5),
                      child: Container(
                          width: cardWidth * 0.8,
                          height: cardWidth * 0.8 * 1.27,
                          child: Image.asset(
                            'assets/images/mahjong/card/south_hand_tile_' +
                                cardInfo.cardId.toString() +
                                '.png',
                            fit: BoxFit.cover,
                          ))),
            ],
          ),
        ),
      ],
    );
  }

  /**
   * 构建当前用户的手牌
   */
  static Widget buildTopUserCard(int cardId) {
    // 玩家的手牌应该将屏幕分为 16等分
    var cardWidth = ScreenUtil().screenWidth / 25;
    var backGroudHeight = cardWidth * 1.46;
    return Column(
      children: [
        Container(
          height: backGroudHeight,
          width: cardWidth,
          child: Stack(
            children: [
              Container(
                  width: double.infinity,
                  height: double.infinity,
                  child: Image.asset(
                    "assets/images/mahjong/card/top_user_card.png",
                    fit: BoxFit.cover,
                  )),
            ],
          ),
        ),
      ],
    );
  }
}
